package es.manuel.maa.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import es.manuel.maa.dao.UserDao;
import es.manuel.maa.model.User;

@Repository(value = UserJpaDaoImpl.ID)
public class UserJpaDaoImpl implements UserDao {
	
	public static final String ID = "userJpaDaoImpl";

	@PersistenceContext
	private EntityManager em;

	@Override
	public User select(Object id) {
		return em.find(User.class, id);
	}

	@Override
	public void insert(User object) {
		em.persist(object);

	}

	@Override
	public User update(User object) {
		return em.merge(object);
	}

	@Override
	public void delete(User object) {
		em.remove(object);
	}

	public User getByUsername(String username) {
		return (User) em.createNamedQuery("getByUsername")
				.setParameter("username", username).getSingleResult();
	}

}
